package com.java.business.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.java.business.dao.MaterialMapper;
import com.java.business.model.Material;
import com.java.business.service.MaterialService;
import com.java.common.model.Result;
import com.java.manage.model.*;
import com.java.manage.service.api.AccessoriesApiService;
import com.java.userservice.model.User;
import com.java.userservice.service.api.UserServiceApi;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class Materialmpl implements MaterialService {

    @Autowired
    private MaterialMapper materialMapper;

    /**
     * 关联配件表
     */
    @DubboReference
    private AccessoriesApiService accessoriesApiService;


    @Transactional
    @Override
    public Result save(Material material) {
        material.setCreateTime(new Date());
        materialMapper.save(material);

        return Result.success();
    }

    @Transactional
    @Override
    public Result modify(Material material) {
        materialMapper.modify(material);

        return Result.success();
    }

    @Transactional
    @Override
    public Result remove(int[] ids) {
        for (int id : ids) {
            materialMapper.remove(id);
        }

        return Result.success();
    }

    @Override
    public Result list(Material material) {
        PageHelper.startPage(material.getPageNum(), material.getPageSize());
        List<Material> list = materialMapper.list(material);

        for (Material materials : list) {
            if (materials.getAccessoriesId() != null) {
                // 配件表
                Accessories accessories = accessoriesApiService.listAll(materials.getAccessoriesId());
                materials.setAccessories(accessories);
            }
        }

        PageInfo<Material> pageInfo = new PageInfo<>(list);

        Map<String, Object> map = new HashMap<>();
        map.put("list", pageInfo.getList());
        map.put("total", pageInfo.getTotal());

        return Result.success(map);
    }

    @Override
    public Result get(int id) {
        Material book = materialMapper.get(id);

        return Result.success(book);
    }


//    //收入
//    @Override
//    public Result saveList(Expenditure expenditure) {
//        expenditure.setType("1");
//        expenditure.setCreateTime(new Date());
//        materialitureMapper.save(expenditure);
//
//        return Result.success();
//    }


}
